﻿using MD5Hash;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class LoginDAL
    {
        public LoginDAL(MyContext myContext)
        {
            MyContext = myContext;
        }

        public MyContext MyContext { get; }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="login"></param>
        /// <returns></returns>
        public int Add(Login login)
        {
            try
            {
                login.Password = login.Password.GetMD5();
                MyContext.logins.Add(login);
                return MyContext.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="Pwd"></param>
        /// <returns></returns>
        public int Login(string phone,string Pwd)
        {
            try
            {
                var n = MyContext.logins.FirstOrDefault(x => x.Phone == phone);
                if(n == null)
                {
                    return 0;
                }
                else
                {
                    if (n.Password == Pwd.GetMD5())
                    {
                        return n.Id;
                    }
                    else
                    {
                        return -1;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public List<Login> Show()
        {
            return MyContext.logins.ToList();
        }
    }
}
